package models;

import java.io.IOException;

import java.sql.SQLException;
import java.util.List;

/** DB persistence **/
import android.content.Context;
import android.location.Geocoder;
import android.location.Address;

import com.j256.ormlite.dao.RuntimeExceptionDao;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.misc.RuntimeExceptionBaseDaoEnabled;
import com.j256.ormlite.table.DatabaseTable;


/**
 * This class holds all the information for a task's Location
 * @author Jonathan Martin
 * @version 1.0
 */
@DatabaseTable(tableName = "locations")
public class Location extends RuntimeExceptionBaseDaoEnabled<Location, Integer> {

	//Instance Data
	@DatabaseField(generatedId = true)
	private int id;

	@DatabaseField
	private String address;

	@DatabaseField
	private double latitude;
	
	@DatabaseField
	private double longitude;
	
	
	
//	/**
//	 * Getter for the user of this category
//	 * @return user
//	 */
//	public User getUser() {
//		return user;
//	}
//	
//	/**
//	 * Setter for user
//	 * @param user
//	 */
//	public void setUser(User user) {
//		this.user = user;
//	}
		
	// For ORMLite DAO access
	private static RuntimeExceptionDao<Location, Integer> defaultDao = null;
	public static void setDefaultDao(RuntimeExceptionDao<Location, Integer> runtimeExceptionDao) {
		defaultDao = runtimeExceptionDao;
	}
	
	/**
	 * Getter for the database access object
	 * @return defaultDao
	 */
	public static RuntimeExceptionDao<Location, Integer> getDefaultDao() {
		return defaultDao;
	}

	/**
	 * Zero-arg constructor, specifically for ORMlite
	 * @throws SQLException 
	 */
	public Location() {
		super();
		setDao((RuntimeExceptionDao<Location, Integer>) defaultDao);
	}
	
	/**
	 * Constructor for category that initializes all the variables
	 * @param name
	 * @param description
	 */
	public Location(String address) {
		this();
		setAddress(address);
	}
	
	/**
	 * Getter for the id of the location
	 * @return id
	 */
	public int getId() {
		return id;
	}
	
	/**
	 * Getter for name of location
	 * @return name
	 */
	public String getAddress() {
		return address;
	}
	
	/**
	 * Sets the name of a location
	 * @param name
	 */
	public void setAddress(String address) {
		this.address = address;
	}
	
	/**
	 * Getter for latitude of location
	 * @return latitude
	 */
	public double getLatitude() {
		return latitude;
	}
	
	/**
	 * Getter for longitude of location
	 * @return longitude
	 */
	public double getLongitude() {
		return longitude;
	}
	
	public boolean ping(Context context) {
		Geocoder coder = new Geocoder(context);
		List<Address> addr;

		try {
		    addr = coder.getFromLocationName(address,5);
		    if (addr == null || addr.size() <= 0) {
		        return false;
		    }
		    Address location = addr.get(0);
		    this.latitude = location.getLatitude();
		    this.longitude = location.getLongitude();
		    return true;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}
	
	
	/**
	 * Convert location to a String object
	 * @return Stringified version
	 */
	public String toString() {
		return address;
	}
}
